#include "SipClnListTrans.hpp"
#include "SipUserAgent.hpp"
#include "SipUserMessage.hpp"

#include "SipGDBodyAdaptor.hpp"
#include "resip/stack/TransactionTerminated.hxx"


#include "rutil/Logger.hxx"
#define RESIPROCATE_SUBSYSTEM Subsystem::APP


namespace wtsip{


CSipClnListTrans::CSipClnListTrans(SipMessage& request, CSipUserAgent& ua):
CSipClnTransaction(request, ua), m_beDestroyed(false)
{
    WarningLog(<< "ListTrans new : " << this);
}


CSipClnListTrans::~CSipClnListTrans()
{
    WarningLog(<< "ListTrans delete : " << this);
}


bool CSipClnListTrans::send(std::auto_ptr<SipMessage> msg)
{
    CSipClnTransaction::send(msg);
    return m_ua.postTimerMessage(new TranTimerMessage(m_tid, true), 5);
}


bool CSipClnListTrans::processIncoming(SipMessage* msg)
{
    if (ClnCompleted == m_State && m_callback)
    {
        m_callback->onResponse(*this, *msg);
    }
    CSipClnTransaction::processIncoming(msg);
    /*if (CSipGDBodyAdaptor::listOver(*msg))
    {
        m_beDestroyed = true;
        m_ua.postSelfMessage(new TransactionTerminated(m_tid, true, &m_ua));
    }*/
    return true;
}


bool CSipClnListTrans::processTimeOut(ApplicationMessage* msg)
{
    if (!msg)
        return false;

    m_beDestroyed = true;
    return m_ua.postSelfMessage(new TransactionTerminated(m_tid, true, &m_ua));
}


bool CSipClnListTrans::canDestroy(){
    return m_beDestroyed;
}


}//namespace

